import { handleActions } from 'redux-actions'
import * as types from '../types/index';
import wepy from 'wepy';
import services from '@/services/index';
let initState = {
  isIndex: 'home',
  activeheadColor: '#fff',
  isActive: false,
  userPassword: '',
  userCellphone: '',
}

export default handleActions({
  [ types.ISINDEX ] (state) {
    return {
      ...state,
      isIndex: state.isIndex = 'home',
    }
  },
  [types.ISMUSIC] (state) {
    return {
      ...state,
      isIndex: state.isIndex = 'music',
    }
  },
  [types.ISVIDEO] (state) {
    return {
      ...state,
      isIndex: state.isIndex = 'video'
    }
  },
  [types.ISACTIVE] (state) {
    return {
      ...state,
      isActive: !state.isActive,
    }
  },
  [types.LOGINUP] (state) {
    wx.clearStorageSync();
    wx.navigateTo({
      url: '/pages/login/index',
    })
    return {
      ...state,
      isActive: !state.isActive,
    }
  },
  [types.SETINFO] (state, { payload } ) {
    let userCellphone = payload.phone;
    let userPassword = payload.pwd;
    let phoneexp = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!phoneexp.test(userCellphone )) {
      wx.showToast({
        title:'请填写正确的手机号码',
        icon: 'none'
      })
      return false;
    }
    wepy.request({
      url: services.login,
      data: {
        phone: userCellphone,
        password: userPassword
      }
    }).then(({data})=>{
      if (data.account.id) {
        wx.setStorageSync('userId', data.account.id);
        wx.navigateTo({
          url: '/pages/home/index',
        })
      }
    }).catch(err=>{
      console.log(err);
    });
    return {
      ...state,
      userPassword: userPassword,
      userCellphone: userCellphone
    }
  }
}, initState)